package dbTest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao2 {
	
	public void add(UserVO user) throws ClassNotFoundException,SQLException{
		
		
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection c = this.getConnection();
		
		PreparedStatement psmt = c.prepareStatement("insert into users(id,name,password) values (?,?,?)");
		psmt.setString(1, user.getId());
		psmt.setString(2,user.getName());
		psmt.setString(3,user.getPassword());
		
		psmt.executeUpdate();
		
		psmt.close();
		c.close();
		
	}
	
	
	public UserVO get(String id) throws ClassNotFoundException,SQLException{
		
		UserVO user = new UserVO();
		
	
		
		Connection c = this.getConnection();
		
		PreparedStatement psmt = c.prepareStatement("select * from users where id = ?");
		
		psmt.setString(1, id);
		
		ResultSet rs =  psmt.executeQuery();
		
		while(rs.next()){
			user.setId(rs.getString("id"));
			user.setName(rs.getString("name"));
			user.setPassword(rs.getString("password"));
		}
		
		
		rs.close();
		psmt.close();
		c.close();
		
		return user;
		
	}
	
	public Connection getConnection() throws ClassNotFoundException,SQLException{
		
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection c = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ex","ksuniry","73031523");
		
		return c ;
	}
	
	
	public static void t(String args[]) throws ClassNotFoundException,SQLException{
		
		UserDao2 userDao = new UserDao2();
		
		UserVO user = new UserVO();
		
		user.setId("test2");
		user.setName("testNm");
		user.setPassword("testPw");
		
		userDao.add(user);
		
		System.out.println("등록성공"+user.getName());
		
		UserVO user2 = new UserVO();
		user2 = userDao.get(user.getId());
		
		System.out.println("호출성공 : "+user2.getName());
		
	}
}
